{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c2bb9a4c-4c71-4c11-9678-9ea21f939b49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 6]\n",
      " [7 8]]\n",
      "[[5 6]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a=np.asmatrix('5 6; 7 8') # 创建矩阵的方式\n",
    "print(a)\n",
    "b=np.asmatrix([[2,3],[5,6]])\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "70ac0913-17b0-4dd9-9064-e2b64b7c4456",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# 使用asmatrix函数创建常见矩阵\n",
    "# 创建一个3*3矩阵\n",
    "d1=np.asmatrix(np.zeros((3,3)))\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8873429e-9183-44f9-9260-bbfc465a3b64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 创建一个2 * 4的1矩阵\n",
    "d2=np.asmatrix(np.ones((2,4)))\n",
    "print(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1add49b4-7132-4114-80a9-b5d6611d6a75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.92923644 0.40807812 0.48871671]\n",
      " [0.24809381 0.71713841 0.43064947]\n",
      " [0.52149518 0.5451331  0.27074664]]\n"
     ]
    }
   ],
   "source": [
    "d3=np.asmatrix(np.random.rand(3,3))  # 0-1之间的随机数\n",
    "print(d3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "988a1313-3d34-44a9-8a59-c9ad24b903ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 2 2 4 2]\n",
      " [3 6 6 2 7]\n",
      " [1 4 4 2 7]]\n"
     ]
    }
   ],
   "source": [
    "d4=np.asmatrix(np.random.randint(1,8,size=(3,5)))\n",
    "print(d4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e886ac47-e3d5-4baa-a323-8ee2e543e244",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0]\n",
      " [0 1]]\n",
      "[[1 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 1 0]\n",
      " [0 0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "# 对角矩阵\n",
    "nd1 = np.asmatrix(np.eye(2,2),dtype=int)\n",
    "print(nd1)\n",
    "nd2 = np.asmatrix(np.eye(4,4),dtype=int)\n",
    "print(nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f0f6955e-b1f2-44cd-a8cc-80d5d64327aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 2 0]\n",
      " [0 0 3]]\n"
     ]
    }
   ],
   "source": [
    "# 对角线矩阵\n",
    "a=[1,2,3]\n",
    "data = np.asmatrix(np.diag(a))\n",
    "print(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
